<template>
    <div>
        <el-container>
            <el-header style="height: 200px;">
                <router-link :to="{name: 'BlogList'}"><img src="../assets/logo.png"></router-link>
            </el-header>
            <br>
            <br>
            <br>
            <el-main>
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="用户名" prop="username">
                        <el-input placeholder="请输入用户名" v-model="ruleForm.username"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input placeholder="请输入密码" v-model="ruleForm.password" show-password></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">立即登录</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-main>
        </el-container>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const that = this
                        this.$axios.post("/login",this.ruleForm).then(function (rest) {

                            // const jwt = rest.headers['authorization']
                            const userInfo = rest.data.data

                            // that.$store.commit("SET_TOKEN",jwt)
                            that.$store.commit("SET_USERINFO",userInfo)

                            //提示消息
                            that.print(rest.data)

                            //跳到博客列表
                            that.$router.push("/")


                        },function (error) {
                            console.log(error)
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            print(data) {
                this.$message({
                    showClose: true,
                    message: data.msg + '，欢迎 ' + data.data.username,
                    type: 'success'
                });
            }
        }
    }
</script>

<style>
    .el-header, .el-main {
        text-align: center;
    }

    .demo-ruleForm{
        max-width: 500px;
        margin: 0 auto;
    }
</style>
